﻿#region Licence
// <copyright file="Helpers/IoHelper.cs" company="Kopigi">
// Copyright © Kopigi 2014
// </copyright>
// ****************************************************************************
// <author>Marc PLESSIS</author>
// <date>11/10/2014</date>
// <project>Nentang.wpf</project>
// <web>http://nentang.codeplex.com</web>
// <license>
// The MIT License (MIT)
// 
// Copyright (c) 2013 Marc Plessis
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// </license>
#endregion
#region using

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Interfaces.Enums;
using Microsoft.Win32;

#endregion

namespace Helpers
{
    /// <summary>
    /// Helper de gestion des extensions de fichier et des classe <see cref="OpenFileDialog"/> et <see cref="SaveFileDialog"/>
    /// </summary>
    public static class IoHelper
    {
        /// <summary>
        /// Permet de choisir les fichiers à associer au dossier
        /// </summary>
        /// <param name="extensionsEnum">
        /// Liste des extensions pour les fichiers voulus
        /// </param>
        /// <param name="multiSelect">
        /// Permet d'indiquer si l'on doit faire une sélection multiple ou non
        /// </param>
        /// <param name="includealltypes">
        /// Permet d'indiquer si l'on fait une ligne de filtre incluant tous les filtres demandés
        /// </param>
        /// <returns>
        /// Une liste des chemins de fichiers choisi par l'utilisateur
        /// </returns>
        public static List<string> BrowseFile(bool multiSelect, bool includealltypes, params FileExtensionEnum[] extensionsEnum)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog
            {
                DefaultExt = "." + FileExtensionBuilder.GetFileExtensionDetail(extensionsEnum[0]).Extension,
                Filter = FileExtensionBuilder.GetFilters(includealltypes, extensionsEnum),
                Multiselect = multiSelect
            };

            var result = dlg.ShowDialog();
            return result == true ? dlg.FileNames.ToList() : new List<string>();
        }

        /// <summary>
        /// Permet de proposer une boite de dialogue d'enregistrement
        /// </summary>
        /// <param name="extensionsEnum">Liste des extensions pour les fichiers voulus</param>
        /// <returns>Le chemin de fichier choisi par l'utilisateur</returns>
        public static string SaveFile(params FileExtensionEnum[] extensionsEnum)
        {
            return SaveFile(string.Empty, extensionsEnum);
        }

        /// <summary>
        /// Permet de proposer une boite de dialogue d'enregistrement
        /// </summary>
        /// <param name="title"></param>
        /// <param name="extensionsEnum">Liste des extensions pour les fichiers voulus</param>
        /// <returns>Le chemin de fichier choisi par l'utilisateur</returns>
        public static string SaveFile(string title = "", params FileExtensionEnum[] extensionsEnum)
        {
            try
            {
                var dlg = new SaveFileDialog()
                {
                    DefaultExt = "." + FileExtensionBuilder.GetFileExtensionDetail(extensionsEnum[0]).Extension,
                    Filter = FileExtensionBuilder.GetFilters(false, extensionsEnum),
                    Title = title
                };

                var result = dlg.ShowDialog();
                return result == true ? dlg.FileName : string.Empty;
            }
            catch (Exception retu)
            {
                Console.WriteLine(retu);
                return string.Empty;
            }
        }

        /// <summary>
        /// Permet de récupérer un tableau de Byte pour enregistrer l'image
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Byte[] GetBytesOfPicture(string path)
        {
            var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            var datas = new byte[fs.Length];
            fs.Read(datas, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
            return datas;
        }

        /// <summary>
        /// Permet de récupérer une image à partir d'un tableau de byte
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static ImageSource GetImageSourceFromBytes(Byte[] datas)
        {
            if (datas == null)
            {
                throw new NullReferenceException();
            }

            var bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = new MemoryStream(datas);
            bi.EndInit();
            return bi;
        }

        /// <summary>
        /// Permet d'obtenir l'extesion d'un fichier
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static string GetExtensionForFile(string filename)
        {
            return Path.GetExtension(filename);
        }
    }
}
